//
//  ArticleViewController.m
//  Style
//
//  Created by dllo on 15/9/23.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "YLArticleViewController.h"
#import "YLArticleTableViewCell.h"
#import "YLDetailViewController.h"
#import "YLHeadScrollViewController.h"
#import "AFNetWorkingW.h"
#import "ArticleTableView_model.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "YLHeadArticleView.h"
#import "YLSearchViewController.h"
#import <AFNetworking.h>
@interface YLArticleViewController ()<UITableViewDelegate, UITableViewDataSource, UIScrollViewDelegate, UICollectionViewDelegateFlowLayout>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableDictionary *mDic;
@property (nonatomic, strong) NSMutableArray *mArr;
@property (nonatomic, strong) NSMutableArray *urlArr;
@property (nonatomic, strong) NSMutableArray *imageArr;
@property (nonatomic, strong) ArticleTableView_model *articleTableView_model;
@property (nonatomic, strong) YLHeadArticleView *articleView;
@property (nonatomic, strong) UIActivityIndicatorView *activity;
@end

@implementation YLArticleViewController
- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.translucent = NO;
    self.tabBarController.tabBar.hidden = NO;
    [self.tableView reloadData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"文章";
    

    /* 搜索 */
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(5, 5, ViewWidth(self.view) - 10, 30);
    button.layer.borderWidth = 1;
    button.layer.cornerRadius = 5;
    button.layer.masksToBounds = YES;
    button.backgroundColor = [UIColor redColor];
    [button setImage:[UIImage imageNamed:@"search"] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(search) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
    

    [self tableViewSubView];
    
    /* 通知中心传值 */
    NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
    [center addObserver:self selector:@selector(push:) name:@"push" object:nil];
    self.activity = [[UIActivityIndicatorView alloc] init];
    self.activity.color = [UIColor blackColor];
    self.activity.frame = CGRectMake(0, 0, 50, 50);
    self.activity.center = self.view.center;
    [self.view addSubview:self.activity];
    [self.activity startAnimating];
    AFNetworkReachabilityManager *mgr = [AFNetworkReachabilityManager sharedManager];
    if (mgr.reachable == YES) {
        [self dataHandle];
    }
}

- (void)search
{
    YLSearchViewController *search = [[YLSearchViewController alloc] init];
    [self.navigationController pushViewController:search animated:YES];
}

/* tableView设置 */
- (void)tableViewSubView
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 40, ViewWidth(self.view), ScreenHeight - 104) style:UITableViewStyleGrouped];
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.backgroundColor = [UIColor whiteColor];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.showsVerticalScrollIndicator = NO;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:self.tableView];
    [self.tableView registerClass:[YLArticleTableViewCell class] forCellReuseIdentifier:@"reuse1"];
}

/* 每个section返回的cell数量 */
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

/* 返回的cell */
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YLArticleTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse1"];
    return cell;
}

/* 轮播图高度 */
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return ScreenHeight / 10 * 3;
}

/* 数据处理 */
- (void)dataHandle
{
    [AFNetWorkingW netWorkWithURL:@"http://api2.pianke.me/read/columns" body:@"auth=&client=1&deviceid=BA920378-52B5-43F2-BE1A-5404120EDD7A&version=3.0.6" resultBlock:^(id result) {
        self.mDic = result;
        NSDictionary *dic = [self.mDic objectForKey:@"data"];
        
        self.imageArr = [NSMutableArray array];
        self.urlArr = [NSMutableArray array];
        NSArray *arr2 = [dic objectForKey:@"carousel"];
        for (int i = 0; i < 3; i++) {
            self.articleTableView_model = [[ArticleTableView_model alloc] init];
            NSString *imageStr = [arr2[i] objectForKey:@"img"];
            NSString *urlStr = [arr2[i] objectForKey:@"url"];
            [self.articleTableView_model setUrl:urlStr];
            if (imageStr != nil) {
                [self.imageArr addObject:imageStr];
            }
            if (self.articleTableView_model != nil) {
                [self.urlArr addObject:self.articleTableView_model];
            }
        }
        
        [self scrollViewSubviews];
        [self.tableView reloadData];
        [self.activity stopAnimating];
    }];

}

#pragma mark ** scrollView相关设置
/* scrollView布局 */
- (void)scrollViewSubviews
{
    NSMutableArray *viewsArray = [@[] mutableCopy];
    for (int i = 0; i < 3; ++i) {
        UIImageView *scrollImageView = [[UIImageView alloc] init];
        [scrollImageView sd_setImageWithURL:[NSURL URLWithString:self.imageArr[i]]  placeholderImage:[UIImage imageNamed:@"zhanweitu"]];
        scrollImageView.frame = CGRectMake(0, 0, ViewWidth(self.view), ScreenHeight / 10 * 3);
        [viewsArray addObject:scrollImageView];
    }
    self.articleView = [[YLHeadArticleView alloc] initWithFrame:CGRectMake(0, 0, ViewWidth(self.view), ScreenHeight / 10 * 3) animationDuration:3];
    self.articleView.fetchContentViewAtIndex = ^UIView *(NSInteger pageIndex){
        return viewsArray[pageIndex];
    };
    self.articleView.totalPagesCount = ^NSInteger(void){
        return 3;
    };
    self.articleView.TapActionBlock = ^(NSInteger pageIndex){
        YLHeadScrollViewController *headVC = [[YLHeadScrollViewController alloc] init];
        headVC.articleTable_model = [self.urlArr objectAtIndex:pageIndex];
        [self.navigationController pushViewController:headVC animated:YES];
    };
    [self.tableView addSubview:self.articleView];
}

/* tableView行高设置 */
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return ScreenWidth;
}

/* 通知中心传值,push到下一个页面 */
- (void)push:(NSNotification *)noti
{
    YLDetailViewController *detail = [[YLDetailViewController alloc] init];
    detail.article_model = noti.object;
    [self.navigationController pushViewController:detail animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
 
    
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
